/***
 * @company YIMAFU Co.
 * @copyright Copyright @2014, YIMAFU Co., Ltd. All right.
 * @since：JDK1.6
 * @version：1.0
 * @author lifajun
 * @see：
 ***/
package cn.com.qingz.core.converter;

import java.beans.PropertyEditorSupport;
import java.util.Date;

import cn.com.qingz.core.utils.DateUtils;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

/**
 * @author lifajun
 * @version : 1.00
 * @copyright Copyright @2014, YIMAFU Co., Ltd. All right.
 * @Create Time : 2012-7-10 上午10:58:15
 * @Description :表单类型转换注册器
 * @History：Editor　　　version　　　Time　　　　　Operation　　　　　　　Description*
 * 
 */
public class DataConverter implements WebBindingInitializer {

	@Override
	public void initBinder(WebDataBinder binder, WebRequest request) {

		// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
		binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
			}

			@Override
			public String getAsText() {
				Object value = getValue();
				return value != null ? value.toString() : "";
			}
		});
		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(DateUtils.parseDate(text));
			}
		});
	}
}